package org.dev.imges;

import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;

public class ImageUtils {

	static {
		System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
	}
	public static BufferedImage matToBufferedImage(String srcpath) {
		Mat grayMat = Imgcodecs.imread(srcpath);
		byte[] data1 = new byte[grayMat.rows() * grayMat.cols() * (int)(grayMat.elemSize())*3];
        grayMat.get(0, 0, data1);
        BufferedImage image1 = new BufferedImage(grayMat.cols(),
                                grayMat.rows(),BufferedImage.TYPE_BYTE_GRAY);
        image1.getRaster().setDataElements(0, 0, grayMat.cols(), grayMat.rows(), data1);
		return image1;
	}
	public static BufferedImage matToBufferedImage(Mat grayMat) {
		byte[] data1 = new byte[grayMat.rows() * grayMat.cols() * (int)(grayMat.elemSize())*3];
        grayMat.get(0, 0, data1);
        BufferedImage image1 = new BufferedImage(grayMat.cols(),
                                grayMat.rows(),BufferedImage.TYPE_3BYTE_BGR);
        image1.getRaster().setDataElements(0, 0, grayMat.cols(), grayMat.rows(), data1);
		return image1;
	}
	public static Mat bufferedImagetoMat(String srcpath) throws IOException {
	     /*BufferedInputStream stream = new BufferedInputStream(new FileInputStream(new File(srcpath)));
		  BufferedImage src = ImageIO.read(stream);
		  Mat grayMat = new Mat(src.getHeight(), src.getWidth(), CvType.CV_8UC3);
		  Raster data = src.getData();
		  data.get
		  grayMat.put(0, 0, null)*/
		  Mat grayMat = Imgcodecs.imread(srcpath);
		  return grayMat;
	}

}
